<?php
class OpportunityManageController extends CAdmin{
	function __construct(){
		parent::__construct();
	}
	
	public function init(){
		$cache = new Caches();
		$option_cache = $cache->getcache('option', 'commons');
		include self::admin_tpl('opportunity_list');
	}
	
	public function ajx_init(){
		$cond = '';
		//搜索条件处理
		$pro = new CProcess();
		$cond = $pro->getSearchFromPost($_POST);
		$opptunitydb = new Opportunity_Model();
		$page = isset($_GET['page']) ? intval($_GET['page']) : '1';
		$userid = $_SESSION['userid'];
		$pagesize = 2;	
		$setype = ROUTE_C;
		$where ="opportunity_id in (select crmid from r_crmentity where (smcreatorid = '$userid' or smsharerid ='$userid') and setype = '$setype' and deleted = 0)".$cond;
		$datas = $opptunitydb->listinfo($where,'opportunity_id,opportunity_name,opportunity_source,opportunity_stage,opport_possibility,opport_status', '', $page, $pagesize);
		$cache = new Caches();
		$option_cache = $cache->getcache('option', 'commons');
		$page_data = array();
		foreach ($datas as $data) {
			$data['opportunity_source'] = $option_cache['opportunity_source'][$data['opportunity_source']];
			$data['opportunity_stage'] = $option_cache['opportunity_stage'][$data['opportunity_stage']];
			$data['opport_possibility'] = $option_cache['opport_possibility'][$data['opport_possibility']];
			$data['opport_status'] = $option_cache['opport_status'][$data['opport_status']];
			$page_data[] = $data;
		}
		$head = "opportunity_name,opportunity_source,opportunity_stage,opport_possibility,opport_status";
		$page_head = explode(',',$head);
		$page_state = $opptunitydb->pages; 		
		$page = array('page_data'=>$page_data,'page_head'=>$page_head,'page_state'=>$page_state);
		echo $infot = json_encode($page);
	}
	
	public function add(){
		if(isset($_POST['dosubmit'])){
//			echo "<pre/>";
//			print_r($_POST);
			$opportunitydb = new Opportunity_Model();
			$usergroupdb = new User_group_Model();
			$crmentitydb = new CrmEntity_Model();
			$info = $this->checkinfo($_POST['info']);
			$new_id = $opportunitydb->insert($info,true);
			$userid = $_SESSION['userid'];
			$setype = ROUTE_C;
			$parentid=$usergroupdb->get_one(array('userid'=>$userid),'parentid');//查找是否有组长
			if($parentid){
				$crmentitydb->insert(array('crmid'=>$new_id,'smcreatorid'=>$userid,'smsharerid'=>$parentid['parentid'],'setype'=>$setype,'createdtime'=>SYS_TIME));	
			} else {
				$crmentitydb->insert(array('crmid'=>$new_id,'smcreatorid'=>$userid,'setype'=>$setype,'createdtime'=>SYS_TIME));
			}
			if(isset($_POST['quote'])) $this->set_quote($new_id, $_POST['quote']);			
			self::showmessage('操作成功！','?m=sale&c=opportunitymanage');
		}else{
			$cache = new Caches();
			$option_cache = $cache->getcache('option', 'commons');
			include self::admin_tpl('opportunity_add');
		}
	}
	
	public function edit(){
		if(isset($_POST['dosubmit'])){
			$opportunitydbdb = new Opportunity_Model();
			$crmentitydb = new CrmEntity_Model(); 
			$info = $this->checkinfo($_POST['info']);
			$opportunity_id = intval($info['opportunity_id']);
			$userid = $_SESSION['userid'];
			print_r($opportunity_id);
			$opportunitydbdb->update($info,array('opportunity_id'=>$opportunity_id));
			$crmentitydb->update(array('modifiedby'=>$userid,'modifiedtime'=>SYS_TIME),array('crmid'=>$opportunity_id,'setype'=>ROUTE_C));
			if(isset($_POST['quote'])) $this->set_quote($opportunity_id, $_POST['quote'],'edit');	
			self::showmessage('操作成功！','?m=sale&c=opportunitymanage');
		} else {
			$cache = new Caches();
			$option_cache = $cache->getcache('option', 'commons');
			$opportunitydb = new Opportunity_Model();
			$info = $opportunitydb->get_one(array('opportunity_id'=>$_GET['opportunity_id']));
			$customerdb = new Customer_Model();
			$customer_name = $customerdb->get_one(array('customer_id'=>$info['customer_id']),'customer_name');
			$contactdb = new Contact_Model();
			$contact_name = $contactdb->get_one(array('contact_id'=>$info['contact_id']),'contact_name');
			$quotedb = new Quote_Model();
			$quotes = $quotedb->select(array('opportunity_id'=>$_GET['opportunity_id']));
			extract($info);
			include self::admin_tpl('opportunity_edit');
		}
	}
	
	public function delete(){
		$opportunity_id = $_POST['opportunity_id'];
		$userid = $_SESSION['userid'];
		$crmentitydb = new CrmEntity_Model();
		$crmentitydb->update(array('deletedby'=>$userid,'deletedtime'=>SYS_TIME,'deleted'=>1),array('crmid'=>$opportunity_id,'setype'=>ROUTE_C));
	}
	
	private function checkinfo($info){
		$validator = new GlobalValidator();
		if(!is_array($info)){
			self::showmessage('参数不完整'); return false;
		}elseif(empty($info['opportunity_name'])){
			self::showmessage('销售机会名称不能空');
		}elseif(empty($info['opportunity_data'])){
			$info['opportunity_data'] = SYS_TIME;
		} else{
			$info['opportunity_data'] = strtotime($info['opportunity_data']);
			$info['opport_sign_data'] = strtotime($info['opport_sign_data']);
		}
		return $info;
	}
	
	/**
	 * 报价单增加
	 * Enter description here ...
	 * @param unknown_type $id
	 * @param unknown_type $quotes
	 * @param unknown_type $a
	 */
	private function set_quote($id,$quotes,$a = 'add'){
		$quotedb = new Quote_Model();
		foreach ($quotes as $q) {
			if($a == 'add' && !$q['del']){
				$quotedb->insert(array('quote_name'=>$q['quote_name'],'opportunity_id'=>$id,'price'=>$q['price']));
			} elseif ($a == 'edit') {
				if((!isset($q['quote_id']) || empty($q['quote_id'])) && (!isset($q['del']) || empty($q['del']))){
					$quotedb->insert(array('quote_name'=>$q['quote_name'],'opportunity_id'=>$id,'price'=>$q['price']));
				} 
				if ((!isset($q['del']) || empty($q['del'])) && !empty($q['quote_id'])){
					$quotedb->update(array('quote_name'=>$q['quote_name'],'price'=>$q['price']),array('quote_id'=>$q['quote_id']));
				} 
				if (isset($q['del']) && isset($q['quote_id']) && !empty($q['quote_id']) && !empty($q['del'])) {
					$quotedb->delete(array('quote_id'=>$q['quote_id']));
				}
			}
		}
	}
	
	/**
	 * 动态取得联系人
	 * Enter description here ...
	 */
	public function get_contact(){
		$customerid = intval($_POST['customer_id']);
		$contactdb = new Contact_Model();
		$userid = $_SESSION['userid'];
		$setype = 'contactmanage';
		$contacts = $contactdb->select("customer_id = $customerid and contact_id in (select crmid from r_crmentity where (smcreatorid = '$userid' or smsharerid ='$userid') and setype = '$setype' and deleted = 0)",'contact_id,contact_name');
		echo json_encode($contacts);
	}
}